<?php

namespace App\Services\MakeGoldRecord;

use Carbon\Carbon;
use App\Models\User;
use App\Models\Product;
use App\Models\GoldSeed;
use App\Models\GoldRound;
use App\Models\ProductSku;
use App\Models\GoldRecord;
use App\Models\GoldProduct;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use Illuminate\Database\Eloquent\Model;

class MakeGoldRecord
{
	protected ?User $user = null;

	protected ?GoldProduct $goldProduct = null;

	protected GoldSeed $goldSeed;

	protected GoldRound $goldRound;

	protected Product $product;

	protected ?ProductSku $sku;

    protected ?GoldRecord $upRecord = null;

	public function __construct($user, $unpaidCacheId)
	{
		$goldProduct = null;

		if ($unpaidCacheId instanceof GoldProduct) {
			$goldProduct = $unpaidCacheId;
		} elseif (Redis::hExists($unpaidCacheId, 'gold_product_id')) {
			$pid = (int)Redis::hGet($unpaidCacheId, 'gold_product_id');
			$goldProduct = $pid > 0 ? GoldProduct::find($pid) : null;
		}

		if ($goldProduct) {
			$this->user = $user instanceof User ? $user : User::find($user);
			$this->goldProduct = $goldProduct;
			$this->goldSeed = $goldProduct->goldSeed;
			$this->goldRound = $goldProduct->goldRound;
			$this->product = $goldProduct->goldSeed->product;
			$this->sku = $goldProduct->goldSeed->product->skus()->first();
            $this->upRecord = $goldProduct->lastValidRecord();
		}
	}

	protected function getUpRecordId()
	{
        // last same gold product record.
        $yesterdayRecord = $this->upRecord;
        return $yesterdayRecord ? $yesterdayRecord->id : 0;
	}

	protected function getRoundNumber()
	{
        if ($this->user->is_mock
            and $this->user->mock_name == 'reclaimer') {
            return $this->upRecord ? $this->upRecord->round : 0;
        } elseif ($this->upRecord) {
            return $this->upRecord->round + 1;
        } else {
            return 1;
        }
	}

	protected function createGoldRecord(): Model
	{
		$data = [
			'gold_seed_id' => $this->goldSeed->id,
			'gold_round_id' => $this->goldRound->id,
			'gold_product_id' => $this->goldProduct->id,
			'product_id' => $this->product->id,
			'product_sku_id' => $this->sku->id,
			'up_gold_record_id' => $this->getUpRecordId(),
			'round' => $this->getRoundNumber(),
			'gold_seed_title' => $this->goldSeed->title,
			'product_title' => $this->product->title,
			'product_cover' => $this->product->cover,
			'sku_name' => $this->sku->name,
			'sku_pic' => $this->sku->pic,
			'sku_attr_value_index' => $this->sku->attr_value_index,
			'sku_attr_value' => $this->sku->getAttrKeyValue(),
			'record_all_price' => $this->goldProduct->allPriceToday(),
			'resell_all_price' => $this->goldProduct->allPriceNext(),
			'pay_net_price' => $this->goldProduct->netPriceToday(),
			'pay_cash_price' => $this->goldProduct->cashPriceToday(),
			'pay_gold_beans' => $this->goldProduct->goldBeansToday(),
			'profit_after_resold' => $this->goldProduct->willGetProfitAfterResold(),
		];

		if ($this->user->is_mock) {
			$data['paid_at'] = Carbon::now();
		}

		return $this->user->goldRecords()->create($data);
	}

	protected function record()
	{
		return DB::transaction(function () {

			if (! $this->goldProduct) return null;

			$record = $this->createGoldRecord();

            $this->user->markRecordCacheAsMade($record);

            return $record;
		});
	}

	public function make()
	{
		return $this->record();
	}
}
